﻿using MediatR;
using Traceability.App.Apilication.Common;
using Traceability.App.Domain;
using Traceability.App.DTOS;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure.Interfance;

namespace Traceability.App.Apilication.Handler
{
    public class TraceabilityHandler : IRequestHandler<TraceabilityCommon, ApiPaimg<TraceabilityDTO>>
    {
        private readonly IBaseRespository<TraceabilityModel> tranceRepo;
        private readonly IBaseRespository<GoodsModel> goodsRepo;

        public TraceabilityHandler(IBaseRespository<TraceabilityModel> tranceRepo,IBaseRespository<GoodsModel> goodsRepo)
        {
            this.tranceRepo = tranceRepo;
            this.goodsRepo = goodsRepo;
        }

        public Task<ApiPaimg<TraceabilityDTO>> Handle(TraceabilityCommon request, CancellationToken cancellationToken)
        {
            ApiPaimg<TraceabilityDTO> res=new ApiPaimg<TraceabilityDTO>();

            var trance = tranceRepo.GetAll().Where(x=>x.Delete_Identifier==false);
            var goods = goodsRepo.GetAll();

            if (!string.IsNullOrEmpty(request.Production_Batch))
            {
                trance = trance.Where(x=>x.Production_Batch.Contains(request.Production_Batch));
            }
            if (request.Quality_Guarantee_Period!=null)
            {
                trance = trance.Where(x => x.Quality_Guarantee_Period==request.Quality_Guarantee_Period);
            }
            if (!string.IsNullOrEmpty(request.Production_License_Number))
            {
                trance = trance.Where(x => x.Production_License_Number.Contains(request.Production_License_Number));
            }
            if (!string.IsNullOrEmpty(request.Manufacturing_Enterprise))
            {
                trance = trance.Where(x => x.Manufacturing_Enterprise.Contains(request.Manufacturing_Enterprise));
            }
            if (!string.IsNullOrEmpty(request.CreateName))
            {
                trance = trance.Where(x => x.CreateName.Contains(request.CreateName));
            }
            if (!string.IsNullOrEmpty(request.UpdateName))
            {
                trance = trance.Where(x => x.UpdateName.Contains(request.UpdateName));
            }
            if (request.CreateTime!=null)
            {
                trance = trance.Where(x => x.CreateTime == request.CreateTime);
            }
            if (!string.IsNullOrEmpty(request.Goods_Name))
            {
                goods = goods.Where(x => x.Goods_Name.Contains(request.Goods_Name));
            }
            var linq = from a in trance
                       join b in goods
                       on a.Product_Name equals b.Goods_ID
                       select new TraceabilityDTO
                       {
                           Traceability_ID=a.Traceability_ID,
                           Product_Name = a.Product_Name,
                           Production_Batch=a.Production_Batch,
                           Quality_Guarantee_Period=a.Quality_Guarantee_Period,
                           Production_License_Number=a.Production_License_Number,
                           Manufacturing_Enterprise=a.Manufacturing_Enterprise,
                           CreateName=a.CreateName,
                           CreateTime=a.CreateTime,
                           UpdateName=a.UpdateName,
                           UpdateTime=a.UpdateTime,
                           Cover_Image=a.Cover_Image,
                           Delete_Identifier=a.Delete_Identifier,
                           Goods_Name=b.Goods_Name,
                       };


            res.ToTaCount = linq.Count();
            res.ToTaPage = (int)Math.Ceiling(res.ToTaCount * 1.0 / request.PageSize);
            res.PageData = linq.OrderByDescending(x => x.Traceability_ID).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(res);


        }
    }
}
